<template>
  <div v-if="props.isShow" class="modal">
    <div class="top">
      <div class="sel">
        <van-icon name="search" />
        <input type="text" v-model="ipt" @input="select" placeholder="搜索大写S / H / L">
        <van-icon name="service-o" />
      </div>
      <button @click="closeModal">取消</button>
    </div>

    <div class="center">
      <div class="list" v-if="filterSearch.length === 0">
        <div class="list_title" v-for="i in classifyData.filter(item => item.type && item.arr.length !== 0)" :key="i.id">
          <p class="title" v-if="i.title !== '最近在搜'">{{ i.title }}</p>
          <div v-else class="zuiJin">
            <p class="title">{{ i.title }}</p>
            <van-icon name="delete-o" @click="delBtn(i)" v-if="!i.btnType" />
            <div class="btn" v-else>
              <button style="background-color: #ececed; color:#6b738e;" @click="empty(i)">清空</button>
              <button style="background-color: #06c060; color: #fff;" @click="finish(i)">完成</button>
            </div>
          </div>
          <div class="list_arr">
            <van-row>
              <van-col :span="i.title === '最近在搜' ? 12 : 6" v-for="j in i.arr ? i.arr.filter(item => item.flag) : []"
                :key="j.id">
                <div :class="i.title === '最近在搜' && j.type ? 'dis' : 'pis'">
                  <p :class="i.title === '最近在搜' ? 'active' : ''"> <van-icon name="underway-o"
                      v-show="i.title === '最近在搜'" /> {{ j.name
                      }}</p>
                  <van-icon name="cross" v-show="i.title === '最近在搜' && j.type" @click="BtnX(j)" />
                </div>
              </van-col>
            </van-row>
          </div>
        </div>
      </div>
      <div class="listText" v-else>
        <ul>
          <li v-for="j in filterSearch" :key="j.id">
            <div class="lianXiRan" v-if="j.type === '联系人'">
              <p>{{ j.type }}</p>
              <p @click="details(j)">{{ j.name }}</p>
            </div>
            <div class="lianXiRan" v-if="j.type === '群聊'">
              <p>{{ j.type }}</p>
              <p @click="details(j)">{{ j.name }}</p>
            </div>
          </li>
          <li v-if="filterSearch.length === 0">无信息</li>
        </ul>
      </div>
    </div>

    <div class="pageSet">
      <p @click="ok">页面设置</p>
    </div>
    <!-- 底部弹框 -->
    <van-popup v-model:show="show" round closeable close-icon-position="top-left" position="bottom"
      :style="{ height: '70%' }">
      <div class="popup_top">
        页面设置
      </div>
      <div class="popup_center">
        <p class="center_title">你可以指定出现在当前丙面展示的功能</p>
        <div class="context">
          <div class="text" v-for="i in classifyData" :key="i.id">
            <div class="text_item">
              <div class="item_foot">
                <p>{{ i.title }}</p>
                <van-switch v-model="i.type" active-color="#07c160" inactive-color="#dcdee0" />
              </div>
              <div class="item_item" v-show="i.type">
                <div v-for="j in i.arr" class="item_lss" :key="j.id">
                  <p> <span>-</span> {{ j.name }}</p>
                  <van-switch v-model="j.flag" active-color="#07c160" inactive-color="#dcdee0" />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>
 
<script setup>
import { ref, reactive, computed } from 'vue'

// 搜索模拟数据 type 1：联系人 2群聊
const search = [
  {
    id: 1,
    name: 'SJJ',
    type: 1
  },
  {
    id: 2,
    name: 'SGQ',
    type: 2
  },
  {
    id: 3,
    name: 'HNT',
    type: 2
  },
  {
    id: 4,
    name: 'LCB',
    type: 2
  }
]

// 存储搜索到数据后点击想要看的信息进行存储
const selectedItem = ref([])

// 分类数据
let classify = [
  {
    id: 1,
    title: '搜索指定内容',
    type: true,
    arr: [
      {
        id: 11,
        name: '朋友圈',
        flag: true
      },
      {
        id: 12,
        name: '小程序',
        flag: false
      },
      {
        id: 13,
        name: '公众号',
        flag: true
      },
      {
        id: 14,
        name: '视频号',
        flag: true
      },
    ]
  },
  {
    id: 2,
    title: '最近在搜',
    type: true,
    btnType: false,
    arr: selectedItem.value,
  }
]

const ipt = ref('')
const show = ref(false)
// 所有信息列表
let classifyData = reactive(classify)
// 存储搜索到数据
const searchData = ref(search)
let timeout = null;

const props = defineProps(['isShow'])
const emits = defineEmits(['closeModal'])

const closeModal = () => {
  emits('closeModal')
}

const ok = () => {
  show.value = true
}

const no = () => {
  show.value = false
}

// 点击搜索到的列表
const details = (data) => {
  const obj = {
    id: new Date().getTime(),
    name: data.name,
    flag: true,
    type: false
  }
  const index = selectedItem.value.findIndex(item => item.name === obj.name);

  if (index !== -1) {
    // 删除旧项
    selectedItem.value.splice(index, 1);
  }

  // 将新的项推到数组的头部
  selectedItem.value.unshift(obj);
}

// 搜索功能
const select = (e) => {
  // 防抖处理
  clearTimeout(timeout);
  timeout = setTimeout(() => {
    ipt.value = e.target.value;
  }, 400); // 设置防抖时间为400毫秒
}

// 计算出所有符合搜索的数据
const filterSearch = computed(() => {
  if (ipt.value === "") return []
  let filteredArr = searchData.value.filter(item => item.name.includes(ipt.value))
  // 使用 map 修改数组元素并返回新数组
  return filteredArr.map(item => {
    if (item.type === 1) item.type = "联系人";
    if (item.type === 2) item.type = "群聊";
    return item; // 返回修改后的元素
  });
});

// 点击删除
const delBtn = (i) => {
  i.btnType = true
  i.arr.forEach(item => {
    item.type = true
  })
}

// 点击清空
const empty = (i) => {
  i.arr.length = 0
}

// 点击完成
const finish = (data) => {
  console.log(data);
  data.btnType = false
  data.arr.forEach(item => {
    item.type = false
  })
}

// 点击X号
const BtnX = (data) => {
  classifyData.forEach(item => {
    if (item.arr) {
      item.arr = item.arr.filter(i => i.id !== data.id);
    }
  });

  console.log(classifyData);
};

if (!props.isShow) clearTimeout(timeout)
</script>
 
<style scoped>
.modal {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: #ededed;
  padding: 0 15px 0 15px;
  z-index: 100;
}

.top {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: space-evenly;
  align-items: center;

  .sel {
    width: 270px;
    height: 30px;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    background-color: #fff;
    border-radius: 10px;

    input {
      border: none;
    }
  }

  button {
    border: none;
    color: #63718e;
  }
}

.center {
  width: 100%;
  height: 100%;

  .list {
    width: 100%;

    .list_title {
      .title {
        width: 100%;
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid #dddddd;
        color: #b3b3b3;
      }

      .zuiJin {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #dcdcdc;

        .btn {
          display: flex;
          width: 100px;
          height: 30px;

          button {
            height: 30px;
            width: 50px;
            border: none;
            border-radius: 7px;
          }
        }
      }

      .list_arr {
        width: 100%;
        height: 45px;
        /* display: flex;
        align-items: center;
        gap: 40px; */

        .active {
          width: 100px;
          padding: 5px;
          background-color: #dcdbdb;
        }

        .dis {
          width: 150px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          background-color: #dcdbdb;
          padding: 5px;
          margin-top: 5px;
          color: #727272;
        }

        .pis {
          width: 80px;
          padding: 5px 0 5px 5px;
          margin-top: 5px;
          color: #606a83;
        }
      }
    }
  }
}

.modal-content {
  width: 100%;
  height: 100%;
}

.popup_top {
  width: 100%;
  height: 50px;
  text-align: center;
  line-height: 50px;
}

.popup_center {
  .center_title {
    height: 30px;
    color: #c0c0c0;
  }

  .context {
    .text {
      .text_item {
        padding: 0 10px 0 10px;

        .item_foot {
          padding: 10px 0 10px 0;
          display: flex;
          justify-content: space-between;
        }

        .item_item {
          span {
            color: #e0e0e0;
          }

          .item_lss {
            display: flex;
            justify-content: space-between;
            /* border-bottom: 1px solid #fafafa; */
            height: 50px;
            /* align-items: center; */
          }
        }
      }
    }
  }
}

.pageSet {
  width: 100%;
  position: relative;
  bottom: 150px;
  text-align: center;
}

.modal-header,
.modal-footer {
  padding: 10px 0;
  text-align: right;
}

.modal-body {
  padding: 20px 0;
}
</style>